package com.itwn.controller;

import com.github.pagehelper.PageInfo;
import com.itwn.entity.Cinema;
import com.itwn.entity.Hall;
import com.itwn.entity.HallType;
import com.itwn.result.ResponseResult;
import com.itwn.service.HallService;
import com.itwn.vo.CinemaVo;
import com.itwn.vo.HallTypeVo;
import com.itwn.vo.HallVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api("影院操作")
@RequestMapping("hall")
@CrossOrigin
public class HallController {
    private HallService hallService;

    @Autowired
    public HallController(HallService hallService) {
        this.hallService = hallService;
    }

    @PostMapping("/searchHall")
    @ApiOperation(value = "分页查询影厅")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query", name = "pageNum", value = "页码",
                    dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(
                    paramType = "query", name = "pageSize", value = "每页多少内容",
                    dataType = "int", defaultValue = "5"),
            @ApiImplicitParam(
                    paramType = "query", name = "hallName", value = "影厅名称",
                    dataType = "String"),
            @ApiImplicitParam(
                    paramType = "query", name = "cinemaName", value = "所属影院关键字",
                    dataType = "String"),
            @ApiImplicitParam(
                    paramType = "query", name = "hallTypeId", value = "影厅类型",
                    dataType = "int")
    })
    public ResponseResult<PageInfo<HallVo>> searchHall(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                       @RequestParam(value = "hallName", required = false) String hallName,
                                                       @RequestParam(value = "cinemaName", required = false) String cinemaName,
                                                       @RequestParam(value = "hallTypeId", required = false) Integer hallTypeId) {
        System.out.println("hallName==" + hallName);
        System.out.println("cinemaName==" + cinemaName);
        System.out.println("hallTypeId==" + hallTypeId);
        ResponseResult<PageInfo<HallVo>> result = new ResponseResult<>();
        try {
            PageInfo<HallVo> pageInfo = hallService.findByCon(pageNum, pageSize, hallName, cinemaName, hallTypeId);
            result.setAll(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setAll(201, "查询失败", null);
        }
        return result;
    }

    @PostMapping("/searchHallType")
    @ApiOperation("查询影厅类型")
    public ResponseResult<List<HallType>> searchHallType() {
        ResponseResult<List<HallType>> result = new ResponseResult<>();
        List<HallType> hallTypeList = hallService.findAllType();
        result.setAll(200, "影厅类型", hallTypeList);
        return result;
    }

    @PostMapping("/addHall")
    @ApiOperation(value = "添加影厅")
    public ResponseResult<String> addHall(@RequestBody Hall hall) {
        ResponseResult<String> result = new ResponseResult<>();
        try{
            int i = hallService.addHall(hall);
            if (i==1){
                result.setAll(200,"添加成功","添加成功");
            }else {
                result.setAll(201,"添加失败","添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setAll(400,"系统异常","系统异常");
        }
        return result;
    }

    @PostMapping("/updateHall")
    @ApiOperation(value = "修改影厅")
    public ResponseResult<String> updateHall(@RequestBody Hall hall) {
        ResponseResult<String> result = new ResponseResult<>();
        try{
            int i = hallService.updateHall(hall);
            if (i==1){
                result.setAll(200,"修改成功","修改成功");
            }else {
                result.setAll(201,"修改失败","修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setAll(400,"系统异常","系统异常");
        }
        return result;
    }

    @PostMapping("/removeHall")
    @ApiOperation(value = "删除影厅")
    public ResponseResult<String> removeHall(@RequestParam(value = "hallId") Integer hallId) {
        ResponseResult<String> result = new ResponseResult<>();
        try{
            int i = hallService.removeHall(hallId);
            if (i==1){
                result.setAll(200,"删除成功","删除成功");
            }else {
                result.setAll(201,"删除失败","删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setAll(400,"系统异常","系统异常");
        }
        return result;
    }

    @GetMapping("/htList")
    public ResponseResult<List<HallTypeVo>> findAllHallTypes(){
        ResponseResult<List<HallTypeVo>> result = new ResponseResult<>();
        List<HallTypeVo> typeList = hallService.findAllHallTypes();
        if(typeList.size()>0){
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(typeList);
        }else{
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }
}
